// order-submit.js
import {ajax} from "../../utils/httpRequest.js";
import {URI_ADDRESSLIST, URI_GETGOODDETAILS, URI_GETWXPAYORDERDATA} from "../../api";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: null,
    hasUserInfo: false,
    goods: {},
    goodsNum: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      options: options,
      goodsNum: options.num
    });
    this.getDetails(options.skuId)
  },
  getDetails(id) {
    ajax.get(URI_GETGOODDETAILS, {
      skuId: id
    }, {}, res => {
      this.setData({
        goods: res.data.data
      })
    });
  },
  orderSubmit2() {
    if (!this.data.hasUserInfo) {
      wx.showModal({
        title: '温馨提示',
        content: '由于海购商品需要绑定身份证号',
        showCancel: false,
        confirmColor: '#ff474f',
        confirmText: "确认",
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/uploadUserPic/uploadUserPic'
            });
          }
        }
      });

    }
  },
  orderSubmit3() {
    if (!this.data.address) {
      wx.showToast({
        title: "请选择收货地址",
        image: "/images/icon-warning.png",
      });
      return;
    }
    this.orderPay();
  },

  //订单提交并支付

  orderPay() {
    ajax.post(URI_GETWXPAYORDERDATA, {
      goodsId: this.data.options.skuId,
      goodsNum: this.data.options.num,
      addressId: this.data.address.id,
      phone: this.data.address.phone,
      consigneeName: this.data.address.userName
    }, {}, res => {
      wx.requestPayment({
        'timeStamp': res.data.data.timeStamp,
        'nonceStr': res.data.data.nonceStr,
        'package': res.data.data.packages,
        'signType': res.data.data.signType,
        'paySign': res.data.data.paySign,
        success: function (e) {
          wx.redirectTo({
            url: "/pages/order/order?status=1",//待发货
          });
        },
        fail: function (e) {
          console.log(e);
          //支付失败转到待支付订单待付款列表
          if (e.errMsg == "requestPayment:fail" || e.errMsg == "requestPayment:fail cancel") {
            wx.showModal({
              title: "提示",
              content: "订单尚未支付",
              showCancel: false,
              confirmText: "确认",
              success: function (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: "/pages/order/order?status=0",
                  });
                }
              }
            });
          }
        }
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var address = wx.getStorageSync("picker_address");
    if (address) {
      this.setData({
        address: address
      });
      wx.removeStorageSync("picker_address");
    } else {
      ajax.get(URI_ADDRESSLIST, {
        isDefaultAddress: 1
      }, {}, res => {
        if (res.data.data.length > 0) {
          this.setData({
            address: res.data.data[0]
            // address: null
          })
        }
      })
    }
  }

});